<?php
App::uses('AppController', 'Controller');
/**
 * 验证码
 * @author zmx@gootop.net  
 * @Date 2012-6-13
 */
class AuthCodeController extends AppController {
	var $uses = array();
	var $autoRender =false;
	public function beforeFilter(){
		$this->Auth->allow('code','getCode');
	}
	
	/**
	 * 返回 png 图片
	 * @author zmx@gootop.net  
	 * @Date 2012-7-20
	 * @return image/png
	 */
	function code(){
		$this->response->type(array('png' => 'image/png'));
		$this->response->type('png');
		App::import('Vendor','AuthCode',array('file' => 'AuthCode' . DS . 'authcode.php'));
		$code = new SecurityCode ();
		$rs = $code->getResult();
		$this->Session->write('register_code',$rs);
		$code->CreateImg ();
	}
	
	/**
	 * 获取验证码
	 * @Date 2013-8-22
	 * @param
	 * @return
	 */
	function getCode($codeType = 'login_code'){
		//ob_clean();
		$this->SetCheckCodes($codeType);
	}
	
	/**
	 * 生成验证码图片
	 * @Date 2013-8-22
	 * @param
	 * @return 
	 */
	private function SetCheckCodes($codeType)
	{
		$x_size=90;
		$y_size=30;
		if(function_exists("imagecreate"))
		{
			$aimg = imagecreate($x_size,$y_size);
			$back = imagecolorallocate($aimg, 255, 255, 255);
			$border = imagecolorallocate($aimg, 0, 0, 0);
			imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
			$txt="123456789ABCDEFGHIJKLMNPKRSTUVWXYZ";
			$txtlen=strlen($txt);
	
			$thetxt="";
			for($i=0;$i<4;$i++)
			{
				$randnum=mt_rand(0,$txtlen-1);
				$randang=mt_rand(-20,20);	//文字旋转角度
				$rndtxt=substr($txt,$randnum,1);
				$thetxt.=$rndtxt;
				$rndx=mt_rand(2,7);
				$rndy=mt_rand(0,9);
				$colornum1=($rndx*$rndx*$randnum)%255;
				$colornum2=($rndy*$rndy*$randnum)%255;
				$colornum3=($rndx*$rndy*$randnum)%255;
				//$newcolor=imagecolorallocate($aimg, $colornum1, $colornum2, $colornum3);
				$newcolor=imagecolorallocate($aimg, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));
				imageString($aimg,5,$rndx+$i*21,5+$rndy,$rndtxt,$newcolor);
			}
			unset($txt);
			$thetxt = strtolower($thetxt);
			if ($codeType=="login"){
				$this->Session->write('login_code',md5($thetxt));
			}elseif ($codeType=="register"){
				$this->Session->write('register_code',md5($thetxt));
			}elseif ($codeType=="forgetpwd"){
				$this->Session->write('forgetpwd_code',md5($thetxt));
			}elseif ($codeType=="admin_login_code"){
				$this->Session->write('admin_login_code',md5($thetxt));
			}else{
				$this->Session->write($codeType,md5($thetxt));
			}
			imagerectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $border);
	
			$newcolor="";
			$newx="";
			$newy="";
			$pxsum=50;	//干扰像素个数
			for($i=0;$i<$pxsum;$i++)
			{
				$newcolor=imagecolorallocate($aimg, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));
				imagesetpixel($aimg,mt_rand(0,$x_size-1),mt_rand(0,$y_size-1),$newcolor);
			}
			header("Pragma:no-cache");
			header("Cache-control:no-cache");
			header("Content-type: image/png");
			imagepng($aimg);
			imagedestroy($aimg);
			exit;
		}
	}
}